package org.abettor.myinj.ui.box;

import org.abettor.myinj.util.I18n;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * 为检测注入点设置关键字
 * @author abettor
 *
 */
public class InjectionPointKeyword extends Dialog
{
	private I18n i18n = new I18n();
	private final static int WIDTH = 300;
	private static int HEIGHT = 200;

	private String keyword;
	private boolean changed = false;
	private Text txtKeyword;

	/**
	 * @see org.eclipse.swt.widgets.Dialog#Dialog(Shell)
	 */
	public InjectionPointKeyword(Shell shell)
	{
		super(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
	}

	public void show()
	{
		Shell parent = getParent();
		Display display = parent.getDisplay();
		Monitor monitor = display.getPrimaryMonitor();
		Rectangle rect = monitor.getBounds();

		//	创建对话框窗口
		final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setText(i18n.getString("box.keyword.title"));
		shell.setSize(WIDTH, HEIGHT);
		Rectangle rectClient = shell.getClientArea();
		HEIGHT = (HEIGHT - rectClient.height) + 30 * 3 + 20 *2;
		shell.setSize(WIDTH, HEIGHT);
		shell.setLocation((rect.width - WIDTH) / 2, (rect.height - HEIGHT) / 2);

		//	关键字标签
		Label label = new Label(shell, SWT.NONE);
		label.setText(i18n.getString("box.keyword.labels.keyword"));
		label.setBounds(30, 30, 50, 20);

		//	关键字
		txtKeyword = new Text(shell, SWT.BORDER);
		txtKeyword.setBounds(80, 30, 180, 20);
		if(keyword != null)
		{
			txtKeyword.setText(keyword);
		}

		//	确定按钮
		Button btnConfirm = new Button(shell, SWT.BORDER);
		btnConfirm.setText(i18n.getString("box.keyword.buttons.confirm.name"));
		btnConfirm.setBounds(80, 80, 50, 20);
		btnConfirm.addMouseListener(new MouseAdapter()
		{
			public void mouseUp(MouseEvent e)
			{
				if(e.button == 1)
				{
					changed = true;
					keyword = txtKeyword.getText();
					shell.close();
				}
			}
		}
		);

		//	取消按钮
		Button btnCancel = new Button(shell, SWT.BORDER);
		btnCancel.setText(i18n.getString("box.keyword.buttons.cancel.name"));
		btnCancel.setBounds(170, 80, 50, 20);
		btnCancel.addMouseListener(new MouseAdapter()
		{
			public void mouseUp(MouseEvent e)
			{
				if(e.button == 1)
				{
					changed = false;
					shell.close();
				}
			}
		}
		);

		//	开始窗口消息循环
		shell.open();
		while (! shell.isDisposed())
		{
			if (! display.readAndDispatch())
			{
				display.sleep();
			}
		}
		shell.dispose();
	}

	/**
	 * 获取关键字
	 */
	public String getKeyword()
	{
		return keyword;
	}

	/**
	 * 设置关键字
	 */
	public void setKeyword(String keyword)
	{
		this.keyword = keyword;
	}

	/**
	 * 检查关键字是否有所改变
	 */
	public boolean isChanged()
	{
		return changed;
	}
}
